{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# calculate PSNR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import os\n",
    "from skimage.measure import compare_psnr, compare_ssim\n",
    "import fnmatch\n",
    "def Result_PSNR(experiment_name, epoch='test_latest'):\n",
    "    result_path = r'./results'\n",
    "    gt_img_path = '/userhome/UnsuDerain/middle_cityRH/testB'\n",
    "#     gt_img_path = os.path.join(result_path, experiment_name, epoch, 'images','B')\n",
    "    image_path = os.path.join(result_path, experiment_name, epoch, 'images','pred_B')\n",
    "    eval_file = os.path.join(result_path, experiment_name, epoch,'eval_file.txt')\n",
    "    aver_psnr = 0.\n",
    "    print(image_path)\n",
    "    b_list = os.listdir(image_path)\n",
    "    b_list = fnmatch.filter(b_list, '*.png')\n",
    "    count = 1\n",
    "    for name in b_list:\n",
    "#         print(name)\n",
    "        pred_B = cv2.imread(os.path.join(image_path, name))\n",
    "        GT_B = cv2.imread(os.path.join(gt_img_path, name))\n",
    "        \n",
    "        try:\n",
    "            tmp_psnr = compare_psnr(pred_B, GT_B)\n",
    "#             print(count, 'tmp_psnr:', tmp_psnr)\n",
    "            aver_psnr = (aver_psnr * count + tmp_psnr) / (count + 1)\n",
    "            count += 1\n",
    "        except:\n",
    "            print('pass:',name)\n",
    "            pass\n",
    "    print('test_num:', count)\n",
    "    print('average PSNR: ', aver_psnr)\n",
    "    with open(eval_file, 'w') as f:\n",
    "        f.write('average PSNR: %f\\n'% aver_psnr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./results/middle_single_ps1/test_latest/images/pred_B\n",
      "pass: cologne_000127_000019_leftImg8bit.png\n",
      "pass: bremen_000076_000019_leftImg8bit.png\n",
      "pass: bremen_000064_000019_leftImg8bit.png\n",
      "test_num: 173\n",
      "average PSNR:  24.13890266295527\n"
     ]
    }
   ],
   "source": [
    "Result_PSNR('middle_single_ps1','test_latest')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# calculate SSIM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./results/cutrain_MidCity_RH/test_latest/images\n",
      "test_num: 1\n",
      "average SSIM:  0.0\n"
     ]
    }
   ],
   "source": [
    "# for old format\n",
    "import cv2\n",
    "import os\n",
    "from skimage.measure import compare_psnr, compare_ssim\n",
    "import fnmatch\n",
    "experiment_name = 'Cycle_MidCity_RH'\n",
    "epoch = 'test_latest'\n",
    "result_path = r'./results'\n",
    "\n",
    "image_path = os.path.join(result_path, experiment_name, epoch, 'images')\n",
    "eval_file = os.path.join(result_path, experiment_name, epoch,'eval_file.txt')\n",
    "aver_ssim = 0.\n",
    "print(image_path)\n",
    "b_list = os.listdir(image_path)\n",
    "b_list = fnmatch.filter(b_list, '*_fake_B.png')\n",
    "count = 1\n",
    "for name in b_list:\n",
    "    little_name = name.replace('fake_B.png', '')\n",
    "        # print(little_name)\n",
    "    pred_B_file = little_name + 'fake_B.png'\n",
    "    B_file = little_name + 'real_B.png'\n",
    "    \n",
    "    pred_B = cv2.imread(os.path.join(image_path, pred_B_file))\n",
    "    GT_B = cv2.imread(os.path.join(image_path, B_file))\n",
    "    try:\n",
    "#         tmp_ssim = compare_ssim(pred_B, GT_B, multichannel=True)\n",
    "        tmp_ssim = compare_psnr(pred_B, GT_B)\n",
    "        print(count, 'tmp_psnr:', tmp_ssim)\n",
    "        aver_ssim = (aver_ssim * count + tmp_ssim) / (count + 1)\n",
    "        count += 1\n",
    "    except:\n",
    "        print('pass:',name)\n",
    "        pass\n",
    "print('test_num:', count)\n",
    "print('average SSIM: ', aver_ssim)\n",
    "with open(eval_file, 'w') as f:\n",
    "    f.write('average SSIM: %f\\n'% aver_ssim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import os\n",
    "from skimage.measure import compare_psnr, compare_ssim\n",
    "import fnmatch\n",
    "def Result_SSIM(experiment_name, epoch='test_latest'):\n",
    "    result_path = r'./results'\n",
    "    gt_img_path = '/userhome/UnsuDerain/middle_cityRH/testB'\n",
    "#     gt_img_path = os.path.join(result_path, experiment_name, epoch, 'images','B')\n",
    "    image_path = os.path.join(result_path, experiment_name, epoch, 'images','pred_B')\n",
    "    eval_file = os.path.join(result_path, experiment_name, epoch,'eval_file.txt')\n",
    "    aver_ssim = 0.\n",
    "    print(image_path)\n",
    "    b_list = os.listdir(image_path)\n",
    "    b_list = fnmatch.filter(b_list, '*.png')\n",
    "    count = 1\n",
    "    for name in b_list:\n",
    "        pred_B = cv2.imread(os.path.join(image_path, name))\n",
    "        GT_B = cv2.imread(os.path.join(gt_img_path, name))\n",
    "        try:\n",
    "            tmp_ssim = compare_ssim(pred_B, GT_B, multichannel=True)\n",
    "#             print('tmp_psnr:', tmp_psnr)\n",
    "            aver_ssim = (aver_ssim * count + tmp_ssim) / (count + 1)\n",
    "            count += 1\n",
    "        except:\n",
    "            print('pass:',name)\n",
    "            pass\n",
    "    print('test_num:', count)\n",
    "    print('average SSIM: ', aver_ssim)\n",
    "    with open(eval_file, 'w') as f:\n",
    "        f.write('average SSIM: %f\\n'% aver_ssim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./results/middle_single_ps1/test_latest/images/pred_B\n",
      "pass: cologne_000127_000019_leftImg8bit.png\n",
      "pass: bremen_000076_000019_leftImg8bit.png\n",
      "pass: bremen_000064_000019_leftImg8bit.png\n",
      "test_num: 173\n",
      "average SSIM:  0.7910859949604918\n"
     ]
    }
   ],
   "source": [
    "Result_SSIM('middle_single_ps1','test_latest')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Function Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "logits_mask = torch.ones_like(mask)\n",
    "print(logits_mask)\n",
    "\n",
    "logits_mask = torch.ones_like(mask).scatter_(1, torch.arange(12).view(-1,1),0)\n",
    "\n",
    "mask * logits_mask\n",
    "\n",
    "exp_logits = torch.tensor([[1,2,3],[2,3,4]])\n",
    "\n",
    "prob = exp_logits.sum(1, keepdim=True)\n",
    "\n",
    "exp_logits - prob\n",
    "\n",
    "num_patches = 3\n",
    "labels = torch.cat([torch.ones(num_patches,1), torch.zeros(num_patches, 1)], dim=0)\n",
    "\n",
    "labels.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Read the size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(667, 1000, 3)\n",
      "(630, 1200, 3)\n",
      "(1200, 1200, 3)\n",
      "(900, 600, 3)\n",
      "(602, 936, 3)\n",
      "(584, 960, 3)\n",
      "(443, 620, 3)\n",
      "(512, 974, 3)\n",
      "(512, 512, 3)\n",
      "(683, 1024, 3)\n",
      "(626, 990, 3)\n",
      "(256, 372, 3)\n",
      "(512, 512, 3)\n",
      "(434, 605, 3)\n",
      "(512, 768, 3)\n",
      "(682, 1024, 3)\n",
      "(900, 1600, 3)\n",
      "(512, 512, 3)\n",
      "(500, 750, 3)\n",
      "(480, 852, 3)\n",
      "(542, 512, 3)\n",
      "(512, 512, 3)\n",
      "(512, 728, 3)\n",
      "(512, 512, 3)\n",
      "(512, 768, 3)\n",
      "(512, 768, 3)\n",
      "(770, 1320, 3)\n",
      "(542, 800, 3)\n",
      "(681, 990, 3)\n",
      "(820, 1500, 3)\n",
      "(450, 720, 3)\n",
      "(512, 768, 3)\n",
      "(512, 682, 3)\n",
      "(933, 1400, 3)\n",
      "(512, 766, 3)\n",
      "(512, 909, 3)\n",
      "(512, 768, 3)\n",
      "(512, 512, 3)\n",
      "(438, 634, 3)\n",
      "(512, 512, 3)\n",
      "(512, 769, 3)\n",
      "(512, 668, 3)\n",
      "(256, 451, 3)\n",
      "(510, 650, 3)\n",
      "(512, 700, 3)\n",
      "(512, 749, 3)\n",
      "(443, 640, 3)\n",
      "(767, 512, 3)\n",
      "(388, 530, 3)\n",
      "(466, 745, 3)\n",
      "(640, 956, 3)\n",
      "(399, 600, 3)\n",
      "(512, 920, 3)\n",
      "(512, 512, 3)\n",
      "(650, 857, 3)\n",
      "(744, 1324, 3)\n",
      "(512, 830, 3)\n",
      "(480, 640, 3)\n",
      "(512, 819, 3)\n",
      "(1414, 2121, 3)\n",
      "(725, 960, 3)\n",
      "(512, 914, 3)\n",
      "(512, 908, 3)\n",
      "(759, 900, 3)\n",
      "(512, 512, 3)\n",
      "(512, 768, 3)\n",
      "(480, 640, 3)\n",
      "(900, 1341, 3)\n",
      "(512, 512, 3)\n",
      "(987, 1316, 3)\n",
      "(534, 1120, 3)\n",
      "(1280, 1280, 3)\n",
      "(939, 1252, 3)\n",
      "(512, 512, 3)\n",
      "(600, 800, 3)\n",
      "(768, 1024, 3)\n",
      "(256, 309, 3)\n",
      "(512, 512, 3)\n",
      "(512, 921, 3)\n",
      "(549, 976, 3)\n",
      "(480, 640, 3)\n",
      "(960, 1280, 3)\n",
      "(600, 960, 3)\n",
      "(512, 745, 3)\n",
      "(410, 608, 3)\n",
      "(512, 512, 3)\n",
      "(512, 512, 3)\n",
      "(720, 960, 3)\n",
      "(600, 800, 3)\n",
      "(512, 512, 3)\n",
      "(512, 967, 3)\n",
      "(512, 512, 3)\n",
      "(659, 960, 3)\n",
      "(444, 650, 3)\n",
      "(362, 495, 3)\n",
      "(512, 771, 3)\n",
      "(600, 800, 3)\n",
      "(683, 1024, 3)\n",
      "(512, 512, 3)\n",
      "(480, 640, 3)\n",
      "(512, 682, 3)\n",
      "(480, 640, 3)\n",
      "(354, 420, 3)\n",
      "(512, 780, 3)\n",
      "(2021, 3000, 3)\n",
      "(512, 512, 3)\n",
      "(720, 1200, 3)\n",
      "(512, 512, 3)\n",
      "(512, 776, 3)\n",
      "(512, 682, 3)\n",
      "(410, 728, 3)\n",
      "(683, 1024, 3)\n",
      "(400, 600, 3)\n",
      "(512, 769, 3)\n",
      "(434, 608, 3)\n",
      "(720, 1280, 3)\n",
      "(2848, 4288, 3)\n",
      "(1063, 1600, 3)\n",
      "(345, 550, 3)\n",
      "(512, 512, 3)\n",
      "(512, 967, 3)\n",
      "(512, 771, 3)\n",
      "(512, 512, 3)\n",
      "(512, 773, 3)\n",
      "(692, 512, 3)\n",
      "(428, 634, 3)\n",
      "(431, 767, 3)\n",
      "(512, 512, 3)\n",
      "(512, 910, 3)\n",
      "(545, 970, 3)\n",
      "(512, 512, 3)\n",
      "(933, 1400, 3)\n",
      "(1080, 1080, 3)\n",
      "(512, 682, 3)\n",
      "(640, 964, 3)\n",
      "(533, 800, 3)\n",
      "(593, 1024, 3)\n",
      "(1000, 1504, 3)\n",
      "(512, 512, 3)\n",
      "(573, 1000, 3)\n",
      "(364, 550, 3)\n",
      "(710, 990, 3)\n",
      "(677, 1024, 3)\n",
      "(630, 1200, 3)\n",
      "(512, 686, 3)\n",
      "(1440, 1920, 3)\n",
      "(512, 914, 3)\n",
      "(256, 386, 3)\n",
      "(512, 512, 3)\n",
      "(267, 400, 3)\n",
      "(512, 928, 3)\n",
      "(512, 779, 3)\n",
      "(1200, 1600, 3)\n",
      "(683, 1024, 3)\n",
      "(512, 682, 3)\n",
      "(854, 1280, 3)\n",
      "(256, 340, 3)\n",
      "(512, 512, 3)\n",
      "(961, 639, 3)\n",
      "(540, 960, 3)\n",
      "(563, 750, 3)\n",
      "(512, 512, 3)\n",
      "(360, 317, 3)\n",
      "(986, 1600, 3)\n",
      "(418, 820, 3)\n",
      "(512, 789, 3)\n",
      "(512, 910, 3)\n",
      "(512, 908, 3)\n",
      "(512, 682, 3)\n",
      "(360, 640, 3)\n",
      "(602, 962, 3)\n",
      "(650, 959, 3)\n",
      "(512, 512, 3)\n",
      "(1066, 1599, 3)\n",
      "(400, 600, 3)\n",
      "(531, 800, 3)\n",
      "(512, 773, 3)\n",
      "(512, 512, 3)\n",
      "(2754, 3935, 3)\n",
      "(576, 1024, 3)\n",
      "(395, 638, 3)\n",
      "(512, 512, 3)\n",
      "(960, 1280, 3)\n",
      "(512, 512, 3)\n",
      "(598, 958, 3)\n",
      "(540, 677, 3)\n",
      "(512, 908, 3)\n",
      "(434, 608, 3)\n",
      "(512, 512, 3)\n",
      "(1067, 1600, 3)\n",
      "(512, 769, 3)\n",
      "(512, 512, 3)\n",
      "(843, 940, 3)\n",
      "(512, 682, 3)\n",
      "(512, 702, 3)\n",
      "(512, 911, 3)\n",
      "(512, 743, 3)\n",
      "(512, 910, 3)\n",
      "(409, 615, 3)\n",
      "(635, 962, 3)\n",
      "(480, 640, 3)\n",
      "(512, 768, 3)\n",
      "(751, 500, 3)\n",
      "(512, 771, 3)\n",
      "(512, 765, 3)\n",
      "(512, 921, 3)\n",
      "(978, 1500, 3)\n",
      "(512, 512, 3)\n",
      "(640, 960, 3)\n",
      "(580, 1068, 3)\n",
      "(659, 964, 3)\n",
      "(768, 1024, 3)\n",
      "(371, 660, 3)\n",
      "(540, 720, 3)\n",
      "(512, 767, 3)\n",
      "(636, 936, 3)\n",
      "(427, 640, 3)\n",
      "(512, 908, 3)\n",
      "(512, 698, 3)\n",
      "(1080, 1920, 3)\n",
      "(512, 512, 3)\n",
      "(399, 600, 3)\n",
      "(600, 800, 3)\n",
      "(443, 640, 3)\n",
      "(455, 810, 3)\n",
      "(600, 750, 3)\n",
      "(435, 580, 3)\n",
      "(480, 640, 3)\n",
      "(512, 770, 3)\n",
      "(600, 800, 3)\n",
      "(365, 680, 3)\n",
      "(512, 769, 3)\n",
      "(512, 910, 3)\n",
      "(512, 778, 3)\n",
      "(512, 967, 3)\n",
      "(467, 700, 3)\n",
      "(845, 1277, 3)\n",
      "(1067, 1600, 3)\n",
      "(466, 1200, 3)\n",
      "(936, 750, 3)\n",
      "(667, 1200, 3)\n",
      "(512, 773, 3)\n",
      "(580, 940, 3)\n",
      "(512, 920, 3)\n",
      "(512, 769, 3)\n",
      "(337, 600, 3)\n",
      "(900, 1200, 3)\n",
      "(512, 769, 3)\n",
      "(551, 980, 3)\n",
      "(480, 640, 3)\n",
      "(407, 600, 3)\n",
      "(512, 825, 3)\n",
      "(512, 768, 3)\n",
      "(512, 512, 3)\n",
      "(343, 770, 3)\n",
      "(387, 620, 3)\n",
      "(349, 623, 3)\n",
      "(640, 964, 3)\n",
      "(512, 851, 3)\n",
      "(636, 1024, 3)\n",
      "(420, 640, 3)\n",
      "(900, 1200, 3)\n",
      "(555, 1000, 3)\n",
      "(532, 800, 3)\n",
      "(531, 800, 3)\n",
      "(623, 960, 3)\n",
      "(1065, 1300, 3)\n",
      "(512, 512, 3)\n",
      "(512, 919, 3)\n",
      "(512, 769, 3)\n",
      "(1000, 2000, 3)\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import cv2\n",
    "img_list = os.listdir(r'../Real_Overcast2/train/Ot')\n",
    "for img_name in img_list:\n",
    "    imgB = cv2.imread(os.path.join(r'../Real_Overcast2/train/Ot',img_name))\n",
    "#     imgO = cv2.imread(os.path.join(r'../Real_Overcast2/train/Os',img_name))\n",
    "#     print(img_name, imgB.shape)\n",
    "    print(img_name)\n",
    "print(len(img_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Resize at max size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(667, 1000, 3) to (512, 767, 3)\n",
      "(630, 1200, 3) to (512, 975, 3)\n",
      "(1200, 1200, 3) to (512, 512, 3)\n",
      "(900, 600, 3) to (768, 512, 3)\n",
      "(602, 936, 3) to (512, 796, 3)\n",
      "(584, 960, 3) to (512, 841, 3)\n",
      "(512, 716, 3) to (512, 716, 3)\n",
      "(512, 974, 3) to (512, 974, 3)\n",
      "(512, 512, 3)\n",
      "(859, 986, 3) to (512, 587, 3)\n",
      "(683, 1024, 3) to (512, 767, 3)\n",
      "(626, 990, 3) to (512, 809, 3)\n",
      "(661, 550, 3) to (615, 512, 3)\n",
      "(512, 744, 3) to (512, 744, 3)\n",
      "(512, 512, 3)\n",
      "(512, 713, 3) to (512, 713, 3)\n",
      "(512, 768, 3) to (512, 768, 3)\n",
      "(682, 1024, 3) to (512, 768, 3)\n",
      "(695, 1685, 3) to (512, 1241, 3)\n",
      "(900, 1600, 3) to (512, 910, 3)\n",
      "(512, 512, 3)\n",
      "(512, 768, 3) to (512, 768, 3)\n",
      "(512, 908, 3) to (512, 908, 3)\n",
      "(542, 512, 3) to (542, 512, 3)\n",
      "(512, 512, 3)\n",
      "(512, 728, 3) to (512, 728, 3)\n",
      "(512, 512, 3)\n",
      "(645, 679, 3) to (512, 538, 3)\n",
      "(512, 768, 3) to (512, 768, 3)\n",
      "(512, 768, 3) to (512, 768, 3)\n",
      "(770, 1320, 3) to (512, 877, 3)\n",
      "(923, 1737, 3) to (512, 963, 3)\n",
      "(542, 800, 3) to (512, 755, 3)\n",
      "(681, 990, 3) to (512, 744, 3)\n",
      "(820, 1500, 3) to (512, 936, 3)\n",
      "(512, 819, 3) to (512, 819, 3)\n",
      "(512, 768, 3) to (512, 768, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(933, 1400, 3) to (512, 768, 3)\n",
      "(512, 766, 3) to (512, 766, 3)\n",
      "(875, 1199, 3) to (512, 701, 3)\n",
      "(512, 909, 3) to (512, 909, 3)\n",
      "(512, 768, 3) to (512, 768, 3)\n",
      "(512, 512, 3)\n",
      "(512, 741, 3) to (512, 741, 3)\n",
      "(512, 512, 3)\n",
      "(512, 769, 3) to (512, 769, 3)\n",
      "(512, 668, 3) to (512, 668, 3)\n",
      "(512, 902, 3) to (512, 902, 3)\n",
      "(512, 652, 3) to (512, 652, 3)\n",
      "(512, 700, 3) to (512, 700, 3)\n",
      "(512, 749, 3) to (512, 749, 3)\n",
      "(512, 739, 3) to (512, 739, 3)\n",
      "(767, 512, 3) to (767, 512, 3)\n",
      "(512, 699, 3) to (512, 699, 3)\n",
      "(512, 818, 3) to (512, 818, 3)\n",
      "(640, 956, 3) to (512, 764, 3)\n",
      "(512, 769, 3) to (512, 769, 3)\n",
      "(512, 920, 3) to (512, 920, 3)\n",
      "(512, 512, 3)\n",
      "(650, 857, 3) to (512, 675, 3)\n",
      "(744, 1324, 3) to (512, 911, 3)\n",
      "(512, 830, 3) to (512, 830, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(576, 512, 3) to (576, 512, 3)\n",
      "(512, 819, 3) to (512, 819, 3)\n",
      "(1414, 2121, 3) to (512, 768, 3)\n",
      "(725, 960, 3) to (512, 677, 3)\n",
      "(512, 914, 3) to (512, 914, 3)\n",
      "(512, 908, 3) to (512, 908, 3)\n",
      "(759, 900, 3) to (512, 607, 3)\n",
      "(512, 512, 3)\n",
      "(512, 768, 3) to (512, 768, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(900, 1341, 3) to (512, 762, 3)\n",
      "(512, 512, 3)\n",
      "(987, 1316, 3) to (512, 682, 3)\n",
      "(534, 1120, 3) to (512, 1073, 3)\n",
      "(1280, 1280, 3) to (512, 512, 3)\n",
      "(939, 1252, 3) to (512, 682, 3)\n",
      "(512, 512, 3)\n",
      "(600, 800, 3) to (512, 682, 3)\n",
      "(768, 1024, 3) to (512, 682, 3)\n",
      "(512, 618, 3) to (512, 618, 3)\n",
      "(656, 1276, 3) to (512, 995, 3)\n",
      "(512, 512, 3)\n",
      "(512, 921, 3) to (512, 921, 3)\n",
      "(549, 976, 3) to (512, 910, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(960, 1280, 3) to (512, 682, 3)\n",
      "(600, 960, 3) to (512, 819, 3)\n",
      "(512, 745, 3) to (512, 745, 3)\n",
      "(512, 759, 3) to (512, 759, 3)\n",
      "(512, 512, 3)\n",
      "(512, 512, 3)\n",
      "(720, 960, 3) to (512, 682, 3)\n",
      "(600, 800, 3) to (512, 682, 3)\n",
      "(512, 512, 3)\n",
      "(512, 967, 3) to (512, 967, 3)\n",
      "(512, 512, 3)\n",
      "(659, 960, 3) to (512, 745, 3)\n",
      "(512, 749, 3) to (512, 749, 3)\n",
      "(512, 700, 3) to (512, 700, 3)\n",
      "(512, 771, 3) to (512, 771, 3)\n",
      "(600, 800, 3) to (512, 682, 3)\n",
      "(683, 1024, 3) to (512, 767, 3)\n",
      "(512, 512, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(825, 1260, 3) to (512, 781, 3)\n",
      "(512, 607, 3) to (512, 607, 3)\n",
      "(512, 780, 3) to (512, 780, 3)\n",
      "(2021, 3000, 3) to (512, 760, 3)\n",
      "(512, 512, 3)\n",
      "(720, 1200, 3) to (512, 853, 3)\n",
      "(512, 512, 3)\n",
      "(512, 776, 3) to (512, 776, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(562, 1658, 3) to (512, 1510, 3)\n",
      "(512, 909, 3) to (512, 909, 3)\n",
      "(683, 1024, 3) to (512, 767, 3)\n",
      "(512, 768, 3) to (512, 768, 3)\n",
      "(512, 769, 3) to (512, 769, 3)\n",
      "(512, 717, 3) to (512, 717, 3)\n",
      "(720, 1280, 3) to (512, 910, 3)\n",
      "(2848, 4288, 3) to (512, 770, 3)\n",
      "(1063, 1600, 3) to (512, 770, 3)\n",
      "(512, 816, 3) to (512, 816, 3)\n",
      "(512, 512, 3)\n",
      "(512, 967, 3) to (512, 967, 3)\n",
      "(512, 771, 3) to (512, 771, 3)\n",
      "(512, 512, 3)\n",
      "(512, 773, 3) to (512, 773, 3)\n",
      "(692, 512, 3) to (692, 512, 3)\n",
      "(512, 758, 3) to (512, 758, 3)\n",
      "(512, 911, 3) to (512, 911, 3)\n",
      "(512, 512, 3)\n",
      "(512, 910, 3) to (512, 910, 3)\n",
      "(545, 970, 3) to (512, 911, 3)\n",
      "(512, 512, 3)\n",
      "(933, 1400, 3) to (512, 768, 3)\n",
      "(1080, 1080, 3) to (512, 512, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(640, 964, 3) to (512, 771, 3)\n",
      "(533, 800, 3) to (512, 768, 3)\n",
      "(593, 1024, 3) to (512, 884, 3)\n",
      "(1000, 1504, 3) to (512, 770, 3)\n",
      "(512, 512, 3)\n",
      "(573, 1000, 3) to (512, 893, 3)\n",
      "(512, 773, 3) to (512, 773, 3)\n",
      "(710, 990, 3) to (512, 713, 3)\n",
      "(677, 1024, 3) to (512, 774, 3)\n",
      "(852, 985, 3) to (512, 591, 3)\n",
      "(630, 1200, 3) to (512, 975, 3)\n",
      "(512, 686, 3) to (512, 686, 3)\n",
      "(1440, 1920, 3) to (512, 682, 3)\n",
      "(512, 914, 3) to (512, 914, 3)\n",
      "(512, 772, 3) to (512, 772, 3)\n",
      "(512, 512, 3)\n",
      "(512, 767, 3) to (512, 767, 3)\n",
      "(512, 928, 3) to (512, 928, 3)\n",
      "(512, 779, 3) to (512, 779, 3)\n",
      "(1200, 1600, 3) to (512, 682, 3)\n",
      "(683, 1024, 3) to (512, 767, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(854, 1280, 3) to (512, 767, 3)\n",
      "(512, 680, 3) to (512, 680, 3)\n",
      "(512, 512, 3)\n",
      "(783, 1433, 3) to (512, 937, 3)\n",
      "(961, 639, 3) to (770, 512, 3)\n",
      "(540, 960, 3) to (512, 910, 3)\n",
      "(563, 750, 3) to (512, 682, 3)\n",
      "(512, 512, 3)\n",
      "(581, 512, 3) to (581, 512, 3)\n",
      "(668, 1709, 3) to (512, 1309, 3)\n",
      "(986, 1600, 3) to (512, 830, 3)\n",
      "(512, 1004, 3) to (512, 1004, 3)\n",
      "(512, 789, 3) to (512, 789, 3)\n",
      "(512, 910, 3) to (512, 910, 3)\n",
      "(512, 908, 3) to (512, 908, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(512, 910, 3) to (512, 910, 3)\n",
      "(602, 962, 3) to (512, 818, 3)\n",
      "(650, 959, 3) to (512, 755, 3)\n",
      "(512, 512, 3)\n",
      "(1066, 1599, 3) to (512, 768, 3)\n",
      "(512, 768, 3) to (512, 768, 3)\n",
      "(531, 800, 3) to (512, 771, 3)\n",
      "(512, 773, 3) to (512, 773, 3)\n",
      "(512, 512, 3)\n",
      "(2754, 3935, 3) to (512, 731, 3)\n",
      "(576, 1024, 3) to (512, 910, 3)\n",
      "(512, 826, 3) to (512, 826, 3)\n",
      "(512, 512, 3)\n",
      "(960, 1280, 3) to (512, 682, 3)\n",
      "(512, 512, 3)\n",
      "(553, 1676, 3) to (512, 1551, 3)\n",
      "(598, 958, 3) to (512, 820, 3)\n",
      "(540, 677, 3) to (512, 641, 3)\n",
      "(512, 908, 3) to (512, 908, 3)\n",
      "(512, 717, 3) to (512, 717, 3)\n",
      "(512, 512, 3)\n",
      "(1067, 1600, 3) to (512, 767, 3)\n",
      "(512, 769, 3) to (512, 769, 3)\n",
      "(512, 512, 3)\n",
      "(512, 957, 3) to (512, 957, 3)\n",
      "(843, 940, 3) to (512, 570, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(512, 702, 3) to (512, 702, 3)\n",
      "(512, 911, 3) to (512, 911, 3)\n",
      "(512, 743, 3) to (512, 743, 3)\n",
      "(512, 910, 3) to (512, 910, 3)\n",
      "(512, 769, 3) to (512, 769, 3)\n",
      "(635, 962, 3) to (512, 775, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(512, 768, 3) to (512, 768, 3)\n",
      "(769, 512, 3) to (769, 512, 3)\n",
      "(512, 771, 3) to (512, 771, 3)\n",
      "(512, 765, 3) to (512, 765, 3)\n",
      "(512, 921, 3) to (512, 921, 3)\n",
      "(978, 1500, 3) to (512, 785, 3)\n",
      "(512, 512, 3)\n",
      "(640, 960, 3) to (512, 768, 3)\n",
      "(580, 1068, 3) to (512, 942, 3)\n",
      "(659, 964, 3) to (512, 748, 3)\n",
      "(768, 1024, 3) to (512, 682, 3)\n",
      "(512, 910, 3) to (512, 910, 3)\n",
      "(540, 720, 3) to (512, 682, 3)\n",
      "(512, 767, 3) to (512, 767, 3)\n",
      "(636, 936, 3) to (512, 753, 3)\n",
      "(512, 767, 3) to (512, 767, 3)\n",
      "(512, 908, 3) to (512, 908, 3)\n",
      "(512, 698, 3) to (512, 698, 3)\n",
      "(942, 1731, 3) to (512, 940, 3)\n",
      "(1080, 1920, 3) to (512, 910, 3)\n",
      "(512, 512, 3)\n",
      "(512, 769, 3) to (512, 769, 3)\n",
      "(600, 800, 3) to (512, 682, 3)\n",
      "(512, 739, 3) to (512, 739, 3)\n",
      "(512, 911, 3) to (512, 911, 3)\n",
      "(600, 750, 3) to (512, 640, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(512, 770, 3) to (512, 770, 3)\n",
      "(600, 800, 3) to (512, 682, 3)\n",
      "(512, 953, 3) to (512, 953, 3)\n",
      "(512, 769, 3) to (512, 769, 3)\n",
      "(512, 910, 3) to (512, 910, 3)\n",
      "(512, 778, 3) to (512, 778, 3)\n",
      "(512, 967, 3) to (512, 967, 3)\n",
      "(512, 767, 3) to (512, 767, 3)\n",
      "(845, 1277, 3) to (512, 773, 3)\n",
      "(1067, 1600, 3) to (512, 767, 3)\n",
      "(512, 1318, 3) to (512, 1318, 3)\n",
      "(936, 750, 3) to (638, 512, 3)\n",
      "(667, 1200, 3) to (512, 921, 3)\n",
      "(512, 773, 3) to (512, 773, 3)\n",
      "(580, 940, 3) to (512, 829, 3)\n",
      "(512, 920, 3) to (512, 920, 3)\n",
      "(512, 769, 3) to (512, 769, 3)\n",
      "(512, 911, 3) to (512, 911, 3)\n",
      "(900, 1200, 3) to (512, 682, 3)\n",
      "(512, 769, 3) to (512, 769, 3)\n",
      "(551, 980, 3) to (512, 910, 3)\n",
      "(512, 682, 3) to (512, 682, 3)\n",
      "(512, 754, 3) to (512, 754, 3)\n",
      "(512, 825, 3) to (512, 825, 3)\n",
      "(512, 768, 3) to (512, 768, 3)\n",
      "(512, 512, 3)\n",
      "(512, 1149, 3) to (512, 1149, 3)\n",
      "(512, 820, 3) to (512, 820, 3)\n",
      "(512, 913, 3) to (512, 913, 3)\n",
      "(640, 964, 3) to (512, 771, 3)\n",
      "(512, 851, 3) to (512, 851, 3)\n",
      "(636, 1024, 3) to (512, 824, 3)\n",
      "(512, 780, 3) to (512, 780, 3)\n",
      "(900, 1200, 3) to (512, 682, 3)\n",
      "(555, 1000, 3) to (512, 922, 3)\n",
      "(532, 800, 3) to (512, 769, 3)\n",
      "(531, 800, 3) to (512, 771, 3)\n",
      "(623, 960, 3) to (512, 788, 3)\n",
      "(1065, 1300, 3) to (512, 624, 3)\n",
      "(512, 512, 3)\n",
      "(512, 919, 3) to (512, 919, 3)\n",
      "(512, 769, 3) to (512, 769, 3)\n",
      "(1000, 2000, 3) to (512, 1024, 3)\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import cv2\n",
    "img_dir = r'../Real_Overcast/testA/'\n",
    "img_list = os.listdir(img_dir)\n",
    "tosize = 512\n",
    "# print(img_list)\n",
    "for img_name in img_list:\n",
    "    img = cv2.imread(os.path.join(img_dir,img_name))\n",
    "    height, width, _ = img.shape\n",
    "    length = max(height, width)\n",
    "    if length > tosize:\n",
    "        if height < width:\n",
    "            new_w = int(tosize*width/height)\n",
    "            new_h = tosize\n",
    "        else:\n",
    "            new_w = tosize\n",
    "            new_h = int(tosize*height/width)\n",
    "#         print(width, height, 'to', new_w,new_h)\n",
    "        img_n = cv2.resize(img, (new_w, new_h))\n",
    "        print(img.shape, 'to', img_n.shape)\n",
    "        cv2.imwrite(os.path.join(img_dir,img_name), img_n)\n",
    "    else:\n",
    "        print(img.shape)\n",
    "        continue\n",
    "#     imgO = cv2.imread(os.path.join(r'../Real_Overcast2/train/Os',img_name))\n",
    "#     print(img_name, imgB.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# delete the .ipy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import shutil\n",
    "img_dir = r'../Real_Overcast/testA/'\n",
    "img_list = os.listdir(img_dir)\n",
    "if '.ipynb_checkpoints' in img_list:\n",
    "    print('it exist!')\n",
    "    os.system('rm -rf ../Real_Overcast/testA/.ipynb_checkpoints')\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "! rm -rf ../Real_Overcast/testA/.ipynb_checkpoints"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# torch learning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "base mask:\n",
      " tensor([[1, 0, 1, 0],\n",
      "        [0, 1, 0, 0],\n",
      "        [1, 0, 1, 0],\n",
      "        [0, 0, 0, 1]], dtype=torch.uint8)\n",
      "repeat mask:\n",
      " tensor([[1, 0, 1, 0, 1, 0, 1, 0],\n",
      "        [0, 1, 0, 0, 0, 1, 0, 0],\n",
      "        [1, 0, 1, 0, 1, 0, 1, 0],\n",
      "        [0, 0, 0, 1, 0, 0, 0, 1],\n",
      "        [1, 0, 1, 0, 1, 0, 1, 0],\n",
      "        [0, 1, 0, 0, 0, 1, 0, 0],\n",
      "        [1, 0, 1, 0, 1, 0, 1, 0],\n",
      "        [0, 0, 0, 1, 0, 0, 0, 1]], dtype=torch.uint8)\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "label = torch.Tensor([[0,1,0,2]])\n",
    "mask = torch.eq(label, label.t())\n",
    "print('base mask:\\n', mask)\n",
    "nviews = 2\n",
    "mask = mask.repeat(nviews, nviews)\n",
    "print('repeat mask:\\n', mask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "diag mask:\n",
      " tensor([[0, 1, 1, 1, 1, 1, 1, 1],\n",
      "        [1, 0, 1, 1, 1, 1, 1, 1],\n",
      "        [1, 1, 0, 1, 1, 1, 1, 1],\n",
      "        [1, 1, 1, 0, 1, 1, 1, 1],\n",
      "        [1, 1, 1, 1, 0, 1, 1, 1],\n",
      "        [1, 1, 1, 1, 1, 0, 1, 1],\n",
      "        [1, 1, 1, 1, 1, 1, 0, 1],\n",
      "        [1, 1, 1, 1, 1, 1, 1, 0]], dtype=torch.uint8)\n"
     ]
    }
   ],
   "source": [
    "batch_size = 4\n",
    "diag_mask = torch.ones_like(mask).scatter_(1,\n",
    "            torch.arange(batch_size * nviews).view(-1, 1),\n",
    "            0)\n",
    "print('diag mask:\\n',diag_mask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
